package com.gxp.system.service.impl;

import com.gxp.common.core.text.Convert;
import com.gxp.common.core.domain.entity.ZkServiceType;
import com.gxp.common.exception.ServiceException;
import com.gxp.common.utils.DateUtils;
import com.gxp.system.mapper.ZkServiceTypeMapper;
import com.gxp.system.mapper.ZkUserSvtypeMapper;
import com.gxp.system.service.IZkServiceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-07
 */
@Service
public class ZkServiceTypeServiceImpl implements IZkServiceTypeService
{
    @Autowired
    private ZkServiceTypeMapper zkServiceTypeMapper;
    @Autowired
    private ZkUserSvtypeMapper userSvtypeMapper;

    /**
     * 查询服务类型
     */
    @Override
    public ZkServiceType selectZkServiceTypeBySvtypeId(Long svtypeId)
    {
        return zkServiceTypeMapper.selectZkServiceTypeBySvtypeId(svtypeId);
    }

    /**
     * 查询服务类型列表
     */
    @Override
    public List<ZkServiceType> selectZkServiceTypeList(ZkServiceType zkServiceType)
    {
        return zkServiceTypeMapper.selectZkServiceTypeList(zkServiceType);
    }

    /**
     * 新增服务类型
     */
    @Override
    public int insertZkServiceType(ZkServiceType zkServiceType)
    {
        zkServiceType.setCreateTime(DateUtils.getNowDate());
        //审核
        zkServiceType.setSvtypeStatus(2l);
        zkServiceType.setApproveType("Add Service Type");
        zkServiceType.setIsLock(0l);
        return zkServiceTypeMapper.insertZkServiceType(zkServiceType);
    }

    /**
     * 修改服务类型
     */
    @Override
    public int updateZkServiceType(ZkServiceType zkServiceType)
    {
        zkServiceType.setUpdateTime(DateUtils.getNowDate());
        zkServiceType.setIsLock(zkServiceType.getSvtypeStatus());
        zkServiceType.setSvtypeStatus(2l);
        zkServiceType.setApproveType("Update Service Type");
        zkServiceType.setComment("");
        return zkServiceTypeMapper.updateZkServiceType(zkServiceType);
    }

    /**
     * 批量删除服务类型
     */
    @Override
    public int deleteZkServiceTypeBySvtypeIds(String svtypeIds)
    {
        Long[] ids = Convert.toLongArray(svtypeIds);
        for (Long svtypeId : ids) {
            ZkServiceType serviceType = selectZkServiceTypeBySvtypeId(svtypeId);
            if(countServiceBySvtypeId(svtypeId) > 0) {
                throw new ServiceException(String.format("%1$s已分配，不能删除", serviceType.getSvtypeName()));
            }

            ZkServiceType zkServiceType = new ZkServiceType();
            zkServiceType.setSvtypeId(svtypeId);
            zkServiceType.setSvtypeStatus(2l);
            zkServiceType.setApproveType("Delete Service Type");
            zkServiceType.setComment("");
            zkServiceTypeMapper.updateZkServiceType(zkServiceType);
        }
        return 1;
    }

    /**
     * 查询service type是否被使用
     * @param svtypeId
     * @return
     */
    private int countServiceBySvtypeId(Long svtypeId) {
        return zkServiceTypeMapper.countServiceBySvtypeId(svtypeId);
    }

    /**
     * 删除服务类型信息
     */
    @Override
    public int deleteZkServiceTypeBySvtypeId(Long svtypeId)
    {
        return zkServiceTypeMapper.deleteZkServiceTypeBySvtypeId(svtypeId);
    }

    /**
     * 查询所有Service Type
     * @return 结果
     */
    @Override
    public List<ZkServiceType> selectSvtypeAll() {
        return zkServiceTypeMapper.selectSvtypeAll();
    }

    /**
     * 查询用户授权的服务类型
     */
    @Override
    public List<ZkServiceType> selectSvtypeByUserId(Long userId) {
        List<ZkServiceType> svtypes = selectSvtypeAll();
        List<ZkServiceType> userSvtypes = zkServiceTypeMapper.selectSvtypesByUserId(userId);
        for (ZkServiceType svtype : svtypes) {
            for (ZkServiceType userSvtype : userSvtypes) {
                if(svtype.getSvtypeId().longValue() == userSvtype.getSvtypeId().longValue()){
                    svtype.setFlag(true);
                    break;
                }
            }
        }
        return svtypes;
    }

    /**
     * 禁用/开启服务类型
     */
    @Override
    public int changeStatus(ZkServiceType svtype) {

        return zkServiceTypeMapper.updateZkServiceType(svtype);
    }

    /**
     * 审核
     * @param zkServiceType
     * @return
     */
    @Override
    public int approve(ZkServiceType zkServiceType) {
        if (zkServiceType.getSvtypeStatus() == 3){
            //审核成功， 状态改为On/Off
            ZkServiceType serviceTypeOld = zkServiceTypeMapper.selectZkServiceTypeBySvtypeId(zkServiceType.getSvtypeId());
            zkServiceType.setSvtypeStatus(serviceTypeOld.getIsLock());
            //判断 Add/Update/Remove
            if(serviceTypeOld.getApproveType().equals("Delete Service Type")){
                //删除用户分配信息
                userSvtypeMapper.deleteZkUserSvtypeBysvtypeId(zkServiceType.getSvtypeId());
                return zkServiceTypeMapper.deleteZkServiceTypeBySvtypeId(zkServiceType.getSvtypeId());
            }
        }
        return zkServiceTypeMapper.updateZkServiceType(zkServiceType);
    }
}
